(function () {
const DONE_VAR = 'DONE';
function whenVMReady(cb) {
let tries = 0;
const tick = setInterval(() => {
const vm = window.vm || (window.Scratch && window.Scratch.vm) || (window.player && window.player.vm);
if (vm && vm.runtime) {
clearInterval(tick);
cb(vm);
} else if (++tries > 300) {
clearInterval(tick);
console.warn('Scratch VM not found.');
}
}, 200);
}
function findVariable(vm, name) {
for (const target of vm.runtime.targets) {
for (const id in target.variables) {
const v = target.variables[id];
if (v && v.name === name) return v;
}
}
return null;
}
function valueOf(varObj) {
return varObj ? varObj.value : 0;
}
whenVMReady(function (vm) {
let sent = false;
const poll = setInterval(() => {
const doneVar = findVariable(vm, DONE_VAR);
const done = valueOf(doneVar);
if (done === 1 && !sent) {
const payload = {
type: 'scratchScore',
petsScore: Number(valueOf(findVariable(vm, 'Pets Score'))),
shoesScore: Number(valueOf(findVariable(vm, 'Shoes Score'))),
familyScore: Number(valueOf(findVariable(vm, 'Family Score'))),
friendsScore: Number(valueOf(findVariable(vm, 'Friends Score'))),
parkScore: Number(valueOf(findVariable(vm, 'Park Score'))),
forestScore: Number(valueOf(findVariable(vm, 'Forest Score'))),
giraffeScore: Number(valueOf(findVariable(vm, 'Giraffe Score'))),
karakScore: Number(valueOf(findVariable(vm, 'Karak Score'))),
quendaScore: Number(valueOf(findVariable(vm, 'Quenda Score'))),
tableScore: Number(valueOf(findVariable(vm, 'Table Score'))),
tigerScore: Number(valueOf(findVariable(vm, 'Tiger Score'))),
treeScore: Number(valueOf(findVariable(vm, 'Tree Score'))),
woodpeckerScore: Number(valueOf(findVariable(vm, 'Woodpecker Score'))),
yongkarScore: Number(valueOf(findVariable(vm, 'Yongkar Score'))),
cityScore: Number(valueOf(findVariable(vm, 'City Score'))),
DONE: 1
};
window.parent.postMessage(payload, '*');
sent = true;
}
}, 200);
});
})();